/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50141
Source Host           : localhost:3306
Source Database       : sport

Target Server Type    : MYSQL
Target Server Version : 50141
File Encoding         : 65001

Date: 2014-02-20 23:26:33
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `clubs_sponsor`
-- ----------------------------
DROP TABLE IF EXISTS `clubs_sponsor`;
CREATE TABLE `clubs_sponsor` (
`sponsorid`  int(11) NOT NULL AUTO_INCREMENT ,
`clubid`  int(11) NOT NULL DEFAULT 0 ,
`name`  varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`name_se`  varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`logo`  varchar(150) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`short`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`description`  text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`tag`  varchar(150) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`status`  tinyint(4) NOT NULL DEFAULT 1 ,
`created`  datetime NOT NULL ,
`modified`  datetime NOT NULL ,
`logid`  int(11) NOT NULL DEFAULT 0 ,
PRIMARY KEY (`sponsorid`)
)
ENGINE=MyISAM
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=1

;

-- ----------------------------
-- Records of clubs_sponsor
-- ----------------------------
BEGIN;
COMMIT;

-- ----------------------------
-- Table structure for `clubs_stadium`
-- ----------------------------
DROP TABLE IF EXISTS `clubs_stadium`;
CREATE TABLE `clubs_stadium` (
`stadiumid`  int(11) NOT NULL AUTO_INCREMENT ,
`clubid`  int(11) NOT NULL ,
`name`  varchar(150) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`name_se`  varchar(150) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`short`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`description`  text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`active`  tinyint(4) NOT NULL DEFAULT 1 COMMENT '0:no/1:yes' ,
`capacity`  int(11) NOT NULL DEFAULT 0 ,
`open`  date NOT NULL ,
`closed`  date NOT NULL ,
`created`  datetime NOT NULL ,
`modified`  datetime NOT NULL ,
`logid`  int(11) NOT NULL ,
`status`  tinyint(4) NOT NULL DEFAULT 1 ,
PRIMARY KEY (`stadiumid`)
)
ENGINE=MyISAM
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=1

;

-- ----------------------------
-- Records of clubs_stadium
-- ----------------------------
BEGIN;
COMMIT;

-- ----------------------------
-- Table structure for `clubs_uniform`
-- ----------------------------
DROP TABLE IF EXISTS `clubs_uniform`;
CREATE TABLE `clubs_uniform` (
`uniformid`  int(11) NOT NULL AUTO_INCREMENT ,
`clubid`  int(11) NOT NULL ,
`y_from`  year NOT NULL ,
`y_to`  year NOT NULL ,
`home_shirt`  tinyint(4) NOT NULL DEFAULT 0 COMMENT '0:no/1:yes' ,
`short`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`description`  text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`created`  datetime NOT NULL ,
`modified`  datetime NOT NULL ,
`status`  tinyint(4) NOT NULL DEFAULT 0 ,
`logid`  int(11) NOT NULL DEFAULT 0 ,
PRIMARY KEY (`uniformid`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=1

;

-- ----------------------------
-- Records of clubs_uniform
-- ----------------------------
BEGIN;
COMMIT;

-- ----------------------------
-- Procedure structure for `clubs_sponsor_add`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_sponsor_add`;

CREATE PROCEDURE `clubs_sponsor_add`(ploginid int,pclubid int,pname varchar(100),plogo varchar(150),pshort varchar(255),pdescription text,ptag varchar(150))
    MODIFIES SQL DATA
BEGIN
	DECLARE rErrCode INT DEFAULT 0;
	DECLARE rSponsorId INT DEFAULT 0;
	DECLARE rNameSe VARCHAR(150) DEFAULT NULL;
	IF rErrCode=0 AND _admin_check_permission(1<<1,ploginid,'club_sponsor')=0 AND ploginid > 0 THEN
		SET rErrCode=1;#no permission
	END IF;
	IF rErrCode = 0 THEN
		SET rNameSe = _com_trim_str(pname);	
		INSERT clubs_sponsor(name,name_se,logo,short,description,
												tag,created,modified,status,logid)
		VALUE(pname,rNameSe,plogo,pshort,pdescription,
					ptag,NOW(),NOW(),1,plogid);
		SET rSponsorId = LAST_INSERT_ID();
	END IF;
	SELECT rErrCode AS errCode, rSponsorId AS sponsorid;
END;

-- ----------------------------
-- Procedure structure for `clubs_sponsor_delete`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_sponsor_delete`;

CREATE PROCEDURE `clubs_sponsor_delete`(ploginid int(11),psponsorid int(11))
    MODIFIES SQL DATA
BEGIN
	DECLARE rErrCode INT DEFAULT 0;
	DECLARE rLogo VARCHAR(150) DEFAULT NULL;
	DECLARE rType TINYINT(4) DEFAULT 0;
	DECLARE rPerm TINYINT(4) DEFAULT 0;
	SET rType = _admin_role(ploginid);
	SET rPerm = _admin_check_permission(1<<3,ploginid,'club_sponsor');
	IF rErrCode=0 AND (rPerm=0 OR rType < 5) THEN
		SET rErrCode=1;#no permission
	END IF;
	IF rErrCode = 0 THEN
		SELECT logo INTO rLogo FROM clubs_sponsor WHERE sponsorid=psponsorid;
		DELETE FROM clubs_sponsor WHERE sponsorid=psponsorid;
	END IF;
	SELECT rErrCode AS errCode, rLogo AS logo;
END;

-- ----------------------------
-- Procedure structure for `clubs_sponsor_get`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_sponsor_get`;

CREATE PROCEDURE `clubs_sponsor_get`(psponsorid int(11))
    READS SQL DATA
BEGIN	
		SELECT *
		FROM clubs_sponsor
		WHERE sponsorid=psponsorid;
END;

-- ----------------------------
-- Procedure structure for `clubs_sponsor_getall_a`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_sponsor_getall_a`;

CREATE PROCEDURE `clubs_sponsor_getall_a`(ploginid int,pstatus tinyint,pclubid int,poption tinyint(4)/**0:all*/,pkeyword varchar(50),pstart date,pend date,poffset int,plimit smallint(6))
    READS SQL DATA
BEGIN
	#call: 1,-1,0,0,'tu+khoa',null,null,0,10
	DECLARE rFoundRows INT DEFAULT 0;
	DECLARE rType TINYINT DEFAULT 0;
	DECLARE rStatus TINYINT DEFAULT 0;
	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_clubs_sponsor(sponsorid bigint);	
	# permission view
	SET rType = _admin_role(ploginid);
	IF rType < 3 THEN SET rStatus = 3;
	ELSE SET rStatus = rType + 1;	
	END IF;
	SET @sqlStm = CONCAT('
												INSERT tmp_clubs_sponsor(sponsorid)
												SELECT SQL_CALC_FOUND_ROWS
														sponsorid
												FROM clubs_sponsor
												WHERE sponsorid IS NOT NULL',	
													IF(pclubid > 0, CONCAT(' AND clubid=',pclubid),''),
													IF(pkeyword !='',
															IF(poption=0,
																 CONCAT(' AND MATCH(name_se,short,tag) AGAINST(',QUOTE(pkeyword),' IN BOOLEAN MODE)'),
																 ELT(poption,
																		CONCAT(' AND name_se LIKE ''%',pkeyword,'%'''),
																		CONCAT(' AND short LIKE ''%',pkeyword,'%'''),
																		CONCAT(' AND tag LIKE ''%',pkeyword,'%''')
																 )
															),
															''
													),
													IF(pstatus > -1, 
														CONCAT(' AND ABS(status)=',pstatus),
														CONCAT(' AND ABS(status)<',rStatus)
													),													
													IF(pstart <> '0000-00-00' AND pend <> '0000-00-00', CONCAT(' AND LEFT(created,10) BETWEEN ''',pstart,''' AND ''',pend,''''),''),
													IF(pstart <> '0000-00-00' AND pend = '0000-00-00', CONCAT(' AND LEFT(created,10)=''',pstart,''''),''),
													IF(pstart = '0000-00-00' AND pend <> '0000-00-00', CONCAT(' AND LEFT(created,10)=''',pend,''''),''),
												' ORDER BY sponsorid DESC
												LIMIT ',poffset,',',plimit
											);

	PREPARE stmt FROM @sqlStm;
	EXECUTE stmt;

	SET rFoundRows = FOUND_ROWS();
	
	SELECT SQL_SMALL_RESULT rFoundRows AS foundRows, B.*
	FROM tmp_clubs_sponsor A JOIN clubs_sponsor B ON A.sponsorid=B.sponsorid;

	DROP TEMPORARY TABLE IF EXISTS tmp_clubs_sponsor;	
END;

-- ----------------------------
-- Procedure structure for `clubs_sponsor_update`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_sponsor_update`;

CREATE PROCEDURE `clubs_sponsor_update`(ploginid int,psponsorid int,pclubid int,pname varchar(100),plogo varchar(150),pshort varchar(255),pdescription text,ptag varchar(150))
    MODIFIES SQL DATA
BEGIN
	DECLARE rErrCode INT DEFAULT 0;
	DECLARE rLogo VARCHAR(150) DEFAULT NULL;
	DECLARE rNameSe VARCHAR(150) DEFAULT NULL;
	IF rErrCode=0 AND _admin_check_permission(1<<2,ploginid,'club_sponsor')=0 AND ploginid > 0 THEN
		SET rErrCode=1;#no permission
	END IF;
	IF rErrCode = 0 THEN
		SELECT logo INTO rLogo FROM clubs_sponsor WHERE sponsorid=psponsorid;
		SET rNameSe = _com_trim_str(pname);	
		UPDATE clubs_sponsor
		SET name=pname,
				name_se=rNameSe,
				logo=IF(plogo != '', plogo, logo),
				short=pshort,
				description=pdescription,
				tag=ptag,
				modified=NOW(),
				logid=plogid
		WHERE sponsorid=psponsorid AND clubid=pclubid;
	END IF;
	SELECT rErrCode AS errCode, rLogo AS logo;
END;

-- ----------------------------
-- Procedure structure for `clubs_sponsor_update_status`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_sponsor_update_status`;

CREATE PROCEDURE `clubs_sponsor_update_status`(ploginid int(11),psponsorid int(11),pstatus tinyint(4),pperm tinyint(4))
    MODIFIES SQL DATA
BEGIN
	DECLARE rErrCode INT DEFAULT 0;
	IF rErrCode=0 AND _admin_check_permission(pperm,ploginid,'club_sponsor')=0 AND ploginid > 0 THEN
		SET rErrCode=1;#no permission
	END IF;
	IF rErrCode=0 THEN		
		UPDATE clubs_sponsor SET `status`=pstatus WHERE sponsorid=psponsorid;	
	END IF;
	SELECT rErrCode AS errCode;
END;

-- ----------------------------
-- Procedure structure for `clubs_stadium_add`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_stadium_add`;

CREATE PROCEDURE `clubs_stadium_add`(ploginid int,pclubid int,pname varchar(100),pshort varchar(255),pdescription text,pactive tinyint(4),pcapacity int,popen date,pclosed date)
    MODIFIES SQL DATA
BEGIN
	DECLARE rErrCode INT DEFAULT 0;
	DECLARE rStadiumId INT DEFAULT 0;
	DECLARE rNameSe VARCHAR(150) DEFAULT NULL;
	IF rErrCode=0 AND _admin_check_permission(1<<1,ploginid,'club_stadium')=0 AND ploginid > 0 THEN
		SET rErrCode=1;#no permission
	END IF;
	IF rErrCode = 0 THEN
		SET rNameSe = _com_trim_str(pname);	
		INSERT clubs_stadium(clubid,name,name_se,short,description,
												active,capacity,`open`,closed,created,
												modified,logid,status)
		VALUE(pclubid,pname,rNameSe,pshort,pdescription,
					pactive,pcapacity,popen,pclosed,NOW(),
					NOW(),plogid,1);
		SET rStadiumId = LAST_INSERT_ID();
	END IF;
	SELECT rErrCode AS errCode, rStadiumId AS stadiumid;
END;

-- ----------------------------
-- Procedure structure for `clubs_stadium_delete`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_stadium_delete`;

CREATE PROCEDURE `clubs_stadium_delete`(ploginid int(11),pstadiumid int(11))
    MODIFIES SQL DATA
BEGIN
	DECLARE rErrCode INT DEFAULT 0;
	DECLARE rType TINYINT(4) DEFAULT 0;
	DECLARE rPerm TINYINT(4) DEFAULT 0;
	SET rType = _admin_role(ploginid);
	SET rPerm = _admin_check_permission(1<<3,ploginid,'club_stadium');
	IF rErrCode=0 AND (rPerm=0 OR rType < 5) THEN
		SET rErrCode=1;#no permission
	END IF;
	IF rErrCode = 0 THEN
		DELETE FROM clubs_stadium WHERE stadiumid=pstadiumid;
	END IF;
	SELECT rErrCode AS errCode;
END;

-- ----------------------------
-- Procedure structure for `clubs_stadium_get`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_stadium_get`;

CREATE PROCEDURE `clubs_stadium_get`(pstadiumid int(11))
    READS SQL DATA
BEGIN	
		SELECT *
		FROM clubs_stadium
		WHERE stadiumid=pstadiumid;
END;

-- ----------------------------
-- Procedure structure for `clubs_stadium_getall_a`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_stadium_getall_a`;

CREATE PROCEDURE `clubs_stadium_getall_a`(ploginid int,pstatus tinyint,pclubid int,poption tinyint(4)/**0:all*/,pkeyword varchar(50),pstart date,pend date,poffset int,plimit smallint(6))
    READS SQL DATA
BEGIN
	#call: 1,-1,0,0,'tu+khoa',null,null,0,10
	DECLARE rFoundRows INT DEFAULT 0;
	DECLARE rType TINYINT DEFAULT 0;
	DECLARE rStatus TINYINT DEFAULT 0;
	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_clubs_stadium(stadiumid bigint);	
	# permission view
	SET rType = _admin_role(ploginid);
	IF rType < 3 THEN SET rStatus = 3;
	ELSE SET rStatus = rType + 1;	
	END IF;
	SET @sqlStm = CONCAT('
												INSERT tmp_clubs_stadium(stadiumid)
												SELECT SQL_CALC_FOUND_ROWS
														stadiumid
												FROM clubs_stadium
												WHERE stadiumid IS NOT NULL',	
													IF(pclubid > 0, CONCAT(' AND clubid=',pclubid),''),
													IF(pkeyword !='',
															IF(poption=0,
																 CONCAT(' AND MATCH(name_se,short) AGAINST(',QUOTE(pkeyword),' IN BOOLEAN MODE)'),
																 ELT(poption,
																		CONCAT(' AND name_se LIKE ''%',pkeyword,'%'''),
																		CONCAT(' AND short LIKE ''%',pkeyword,'%''')																		
																 )
															),
															''
													),
													IF(pstatus > -1, 
														CONCAT(' AND ABS(status)=',pstatus),
														CONCAT(' AND ABS(status)<',rStatus)
													),													
													IF(pstart <> '0000-00-00' AND pend <> '0000-00-00', CONCAT(' AND LEFT(created,10) BETWEEN ''',pstart,''' AND ''',pend,''''),''),
													IF(pstart <> '0000-00-00' AND pend = '0000-00-00', CONCAT(' AND LEFT(created,10)=''',pstart,''''),''),
													IF(pstart = '0000-00-00' AND pend <> '0000-00-00', CONCAT(' AND LEFT(created,10)=''',pend,''''),''),
												' ORDER BY stadiumid DESC
												LIMIT ',poffset,',',plimit
											);

	PREPARE stmt FROM @sqlStm;
	EXECUTE stmt;

	SET rFoundRows = FOUND_ROWS();
	
	SELECT SQL_SMALL_RESULT rFoundRows AS foundRows, B.*
	FROM tmp_clubs_stadium A JOIN clubs_stadium B ON A.stadiumid=B.stadiumid;

	DROP TEMPORARY TABLE IF EXISTS tmp_clubs_stadium;	
END;

-- ----------------------------
-- Procedure structure for `clubs_stadium_update`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_stadium_update`;

CREATE PROCEDURE `clubs_stadium_update`(ploginid int,pstadiumid int,pclubid int,pname varchar(100),pshort varchar(255),pdescription text,pactive tinyint(4),pcapacity int,popen date,pclosed date)
    MODIFIES SQL DATA
BEGIN
	DECLARE rErrCode INT DEFAULT 0;
	DECLARE rNameSe VARCHAR(150) DEFAULT NULL;
	IF rErrCode=0 AND _admin_check_permission(1<<2,ploginid,'club_stadium')=0 AND ploginid > 0 THEN
		SET rErrCode=1;#no permission
	END IF;
	IF rErrCode = 0 THEN
		SET rNameSe = _com_trim_str(pname);	
		UPDATE clubs_stadium
		SET name=pname,
				name_se=rNameSe,
				short=pshort,
				description=pdescription,
				active=pactive,
				capacity=pcapacity,
				`open`=popen,
				closed=pclosed,
				modified=NOW(),
				logid=plogid
		WHERE stadiumid=pstadiumid AND clubid=pclubid;
	END IF;
	SELECT rErrCode AS errCode;
END;

-- ----------------------------
-- Procedure structure for `clubs_stadium_update_status`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_stadium_update_status`;

CREATE PROCEDURE `clubs_stadium_update_status`(ploginid int(11),pstadiumid int(11),pstatus tinyint(4),pperm tinyint(4))
    MODIFIES SQL DATA
BEGIN
	DECLARE rErrCode INT DEFAULT 0;
	IF rErrCode=0 AND _admin_check_permission(pperm,ploginid,'club_stadium')=0 AND ploginid > 0 THEN
		SET rErrCode=1;#no permission
	END IF;
	IF rErrCode=0 THEN		
		UPDATE clubs_stadium SET `status`=pstatus WHERE stadiumid=pstadiumid;	
	END IF;
	SELECT rErrCode AS errCode;
END;

-- ----------------------------
-- Procedure structure for `clubs_uniform_add`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_uniform_add`;

CREATE PROCEDURE `clubs_uniform_add`(ploginid int,pclubid int,py_from year,py_to year,phome_shirt tinyint(4),pshort varchar(255),pdescription text)
    MODIFIES SQL DATA
BEGIN
	DECLARE rErrCode INT DEFAULT 0;
	DECLARE rUniformId INT DEFAULT 0;
	DECLARE rNameSe VARCHAR(150) DEFAULT NULL;
	IF rErrCode=0 AND _admin_check_permission(1<<1,ploginid,'club_uniform')=0 AND ploginid > 0 THEN
		SET rErrCode=1;#no permission
	END IF;
	IF rErrCode = 0 THEN
		SET rNameSe = _com_trim_str(pname);	
		INSERT clubs_stadium(clubid,y_from,y_to,home_shirt,short,
												description,created,modified,status,logid)
		VALUE(pclubid,py_from,py_to,phome_shirt,pshort,
					pdescription,NOW(),NOW(),1,plogid);
		SET rUniformId = LAST_INSERT_ID();
	END IF;
	SELECT rErrCode AS errCode, rUniformId AS uniformid;
END;

-- ----------------------------
-- Procedure structure for `clubs_uniform_delete`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_uniform_delete`;

CREATE PROCEDURE `clubs_uniform_delete`(ploginid int(11),psponsorid int(11))
    MODIFIES SQL DATA
BEGIN
	DECLARE rErrCode INT DEFAULT 0;
	DECLARE rType TINYINT(4) DEFAULT 0;
	DECLARE rPerm TINYINT(4) DEFAULT 0;
	SET rType = _admin_role(ploginid);
	SET rPerm = _admin_check_permission(1<<3,ploginid,'club_uniform');
	IF rErrCode=0 AND (rPerm=0 OR rType < 5) THEN
		SET rErrCode=1;#no permission
	END IF;
	IF rErrCode = 0 THEN		
		DELETE FROM clubs_uniform WHERE uniformid=puniformid;
	END IF;
	SELECT rErrCode AS errCode;
END;

-- ----------------------------
-- Procedure structure for `clubs_uniform_get`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_uniform_get`;

CREATE PROCEDURE `clubs_uniform_get`(puniformid int(11))
    READS SQL DATA
BEGIN	
		SELECT *
		FROM clubs_uniform
		WHERE uniformid=puniformid;
END;

-- ----------------------------
-- Procedure structure for `clubs_uniform_getall_a`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_uniform_getall_a`;

CREATE PROCEDURE `clubs_uniform_getall_a`(ploginid int,pstatus tinyint,pclubid int,pstart date,pend date,poffset int,plimit smallint(6))
    READS SQL DATA
BEGIN
	#call: 1,-1,0,null,null,0,10
	DECLARE rFoundRows INT DEFAULT 0;
	DECLARE rType TINYINT DEFAULT 0;
	DECLARE rStatus TINYINT DEFAULT 0;
	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_clubs_uniform(uniformid bigint);	
	# permission view
	SET rType = _admin_role(ploginid);
	IF rType < 3 THEN SET rStatus = 3;
	ELSE SET rStatus = rType + 1;	
	END IF;
	SET @sqlStm = CONCAT('
												INSERT tmp_clubs_uniform(uniformid)
												SELECT SQL_CALC_FOUND_ROWS
														uniformid
												FROM clubs_uniform
												WHERE uniformid IS NOT NULL',	
													IF(pclubid > 0, CONCAT(' AND clubid=',pclubid),''),													
													IF(pstatus > -1, 
														CONCAT(' AND ABS(status)=',pstatus),
														CONCAT(' AND ABS(status)<',rStatus)
													),													
													IF(pstart <> '0000-00-00' AND pend <> '0000-00-00', CONCAT(' AND LEFT(created,10) BETWEEN ''',pstart,''' AND ''',pend,''''),''),
													IF(pstart <> '0000-00-00' AND pend = '0000-00-00', CONCAT(' AND LEFT(created,10)=''',pstart,''''),''),
													IF(pstart = '0000-00-00' AND pend <> '0000-00-00', CONCAT(' AND LEFT(created,10)=''',pend,''''),''),
												' ORDER BY uniformid DESC
												LIMIT ',poffset,',',plimit
											);

	PREPARE stmt FROM @sqlStm;
	EXECUTE stmt;

	SET rFoundRows = FOUND_ROWS();
	
	SELECT SQL_SMALL_RESULT rFoundRows AS foundRows, B.*
	FROM tmp_clubs_uniform A JOIN clubs_uniform B ON A.uniformid=B.uniformid;

	DROP TEMPORARY TABLE IF EXISTS tmp_clubs_uniform;	
END;

-- ----------------------------
-- Procedure structure for `clubs_uniform_update`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_uniform_update`;

CREATE PROCEDURE `clubs_uniform_update`(ploginid int,puniformid int,pclubid int,py_from year,py_to year,phome_shirt tinyint(4),pshort varchar(255),pdescription text)
    MODIFIES SQL DATA
BEGIN
	DECLARE rErrCode INT DEFAULT 0;
	IF rErrCode=0 AND _admin_check_permission(1<<2,ploginid,'club_uniform')=0 AND ploginid > 0 THEN
		SET rErrCode=1;#no permission
	END IF;
	IF rErrCode = 0 THEN
		UPDATE clubs_uniform
		SET y_from=py_from,
				y_to=py_to,
				home_shirt=phome_shirt,
				short=pshort,
				description=pdescription,
				modified=NOW(),
				logid=plogid
		WHERE uniformid=puniformid AND clubid=pclubid;
		
	END IF;
	SELECT rErrCode AS errCode;
END;

-- ----------------------------
-- Procedure structure for `clubs_uniform_update_status`
-- ----------------------------
DROP PROCEDURE IF EXISTS `clubs_uniform_update_status`;

CREATE PROCEDURE `clubs_uniform_update_status`(ploginid int(11),puniformid int(11),pstatus tinyint(4),pperm tinyint(4))
    MODIFIES SQL DATA
BEGIN
	DECLARE rErrCode INT DEFAULT 0;
	IF rErrCode=0 AND _admin_check_permission(pperm,ploginid,'club_uniform')=0 AND ploginid > 0 THEN
		SET rErrCode=1;#no permission
	END IF;
	IF rErrCode=0 THEN		
		UPDATE clubs_uniform SET `status`=pstatus WHERE uniformid=puniformid;	
	END IF;
	SELECT rErrCode AS errCode;
END;

-- ----------------------------
-- Auto increment value for `clubs_sponsor`
-- ----------------------------
ALTER TABLE `clubs_sponsor` AUTO_INCREMENT=1;

-- ----------------------------
-- Auto increment value for `clubs_stadium`
-- ----------------------------
ALTER TABLE `clubs_stadium` AUTO_INCREMENT=1;

-- ----------------------------
-- Auto increment value for `clubs_uniform`
-- ----------------------------
ALTER TABLE `clubs_uniform` AUTO_INCREMENT=1;

DROP TRIGGER IF EXISTS `clubs_on_delete_a`;
CREATE TRIGGER `clubs_on_delete_a` AFTER DELETE ON `clubs` FOR EACH ROW BEGIN
     DECLARE rClubId INT DEFAULT 0;
     SET rClubId = OLD.clubid;
     # delete clubs_sponsor
     DELETE FROM clubs_sponsor WHERE clubid=rClubId;
     # delete clubs_stadium
     DELETE FROM clubs_stadium WHERE clubid=rClubId;
     # delete clubs_uniform
     DELETE FROM clubs_uniform WHERE clubid=rClubId;
END;
